###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/Update.pm.pl -> Update module                           #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::Update;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use HTML::Dialog;
use HTML::Form;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (input => undef, error => "", @_);

  my $LANGUAGE = {
    "MISSING" => {
      "SERVER"        => qq~<li>You didn't fill in the "Update Server" field.~,
      "USERNAME"      => qq~<li>You didn't fill in the "Registration Username" field.~,
      "PASSWORD"      => qq~<li>You didn't fill in the "Registration Password" field.~
    },
    "INVALID" => {
      "LOGIN"         => qq~<li>The Username and Password you entered are invalid.~
    },
    "CONN-ERROR"    => qq~<li>There was an error connecting to the Update Server.~,
    "ERROR"         => qq~<p><font class="error-body">There were errors:<ul>[%error%]</ul></font>~
  };

  $in{'error'} = &Standard::ProcessError(LANGUAGE => $LANGUAGE, ERROR => $in{'error'});

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body = $Dialog->TableRow(items => [{ body => "<font color=\"Red\">It is recommended that you backup your database before continuing.</font><p>To view the list of updates, first enter the following information:", colspan => 2 }]);

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_SERVER",
    value     => "update.plasmapulse.com",
    subject   => "Update Server",
    required  => 1
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_USERNAME",
    subject   => "Customer ID",
    required  => 1
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_PASSWORD",
    subject   => "Customer Password",
    required  => 1
  );

  $Body .= $Dialog->FormButton(
    items => [
      { type => "submit", value => "Next >" },
      { type => "reset", value => "Cancel" }
    ], join => "&nbsp;"
  );

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Update SuperLinks",
    form    => 1,
    hiddens => [
      { name => "action", value => "ViewUpdate" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ]
  );
  
  return $Body;
}

###############################################################################
# view subroutine
sub view {
  my $self = shift;
  my %in = (input => undef, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();
  my $Form   = HTML::Form->new();

  my $Body = $Dialog->TableHeader(
    items => [
      { title => "", width => "1" },
      { title => "Version", width => "100%" }
    ]
  );

  $Body .= $Dialog->TableRow(items => [{ body => "Select a version to update to:", colspan => 2 }]);
  
  foreach my $version (@{ $in{'input'}->{'VERSIONS'} }) {
    $Body .= $Dialog->TableRow(
      items => [
        { body => $Form->Radio(name => "FORM_VERSION", items => [{ value => $version->{'VERSION'} }]) },
        { body => $version->{'DESCRIPTION'} }
      ]
    );
  }

  $Body .= $Dialog->TableRow(
    items => [{ body => $Form->Button(
      items => [
        { type => "submit", value => "Update" },
        { type => "reset", value => "Cancel" }
      ], join => "&nbsp;"
    ), colspan => 2 }]
  );

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Update SuperLinks",
    form    => 1,
    hiddens => [
      { name => "FORM_SERVER", value => $SL::QUERY{'FORM_SERVER'} },
      { name => "FORM_USERNAME", value => $SL::QUERY{'FORM_USERNAME'} },
      { name => "FORM_PASSWORD", value => $SL::QUERY{'FORM_PASSWORD'} },
      { name => "action", value => "DoUpdate" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ]
  );
  
  return $Body;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my ($section) = @_;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body  = $Dialog->TableRow(items => [{ body => "Updating SuperLinks:", colspan => 2 }]);
     $Body .= $Dialog->TableRow(items => [{ body => "<iframe src=\"".$SL::CGI->self_url()."&do=1\" name=\"LOG\" height=\"300\" width=\"100%\"></iframe>", colspan => 2 }]);

  $Body .= $Dialog->FormButton(
    items => [
      { type => "button", value => "Close", extra => { onClick => "window.location='".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Summary&SID=$SL::ADMIN{'SID'}")."';" } }
    ]
  );
  
  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body        => $Body,
    header      => "Update SuperLinks",
    body_extra  => "onLoad=\"document.LOG.scrollTo(0, 9999999);\""
  );
  
  return $Body;
}

1;